import DayJs from 'dayjs'

const _localStorage = window.localStorage

export const setWithExpires = (key, value) => {
  const data = {
    value,
    expires: `${DayJs().format('YYYY-MM-DD')} 23:59:59`
  }

  _localStorage.setItem(key, JSON.stringify(data))
}

export const getWithExpires = (key) => {
  const value = _localStorage.getItem(key)
  if (!value) {
    return null
  }

  const data =JSON.parse(value)
  if (DayJs().isAfter(DayJs(data.expires))) {
    _localStorage.removeItem(key)
    return null
  }

  return data.value
}